package com.eiss.service.impl;

import com.eiss.common.core.domain.AjaxResult;
import com.eiss.domain.DTO.stationUser;
import com.eiss.domain.Entily.User;
import com.eiss.domain.VO.RegisterVO;
import com.eiss.mapper.UserMapper;
import com.eiss.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.SpringSecurityCoreVersion;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.provisioning.UserDetailsManager;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.regex.Pattern;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public List<stationUser> getStationUser() {
        List<stationUser> stationUserList = userMapper.getStationUser();
        return stationUserList;
    }

    @Override
    public List<stationUser> getTransferStationUser() {
        List<stationUser> stationUserList =userMapper.getTransferStationUser();
        return stationUserList;
    }

    @Override
    public AjaxResult register(RegisterVO registerVO) {
        String username=registerVO.getUserName();
        String password=registerVO.getPassWord();
        Pattern passswordPter = Pattern.compile("^[a-zA-Z0-9\\p{Punct}]{2,10}$");
        Pattern usernamePter = Pattern.compile("^[a-zA-Z0-9]{2,20}$");
        if(!usernamePter.matcher(username).matches()){
            return AjaxResult.error("用户名不符合要求,请输入2-20位的数字英文!");
        }
        if(!passswordPter.matcher(password).matches()){
            return AjaxResult.error("密码不符合要求,请输入2-10位的数字英文特殊字符!");
        }
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        registerVO.setPassWord(encoder.encode(password));
        User user = User.builder()
                .NickName(registerVO.getNickName())
                .Email(registerVO.getEmail())
                .UserName(registerVO.getUserName())
                .PassWord(registerVO.getPassWord())
                .Gender(registerVO.getGender())
                .Phone(registerVO.getPhone())
                .LogingIp("")
                .UserType("01")
                .avatar("")
                .status(0)
                .Delflag(0)
                .CreateTime(String.valueOf(LocalDateTime.now()))
                .UpdateTime(String.valueOf(LocalDateTime.now()))
                .build();
        BeanUtils.copyProperties(user, registerVO);
        log.info("数据:{}",user);
        int sum=userMapper.register(user);
        if(sum<=0){
            return AjaxResult.error("注册失败,用户名重复,请重新输入!");
        }
        return AjaxResult.success("注册成功!");
    }
}
